package com.example.dbcms.service.impl;


import com.example.dbcms.domain.Residents;
import com.example.dbcms.mapper.ResidentDao;
import com.example.dbcms.service.ResidentService;
import com.example.dbcms.utils.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.List;
import java.util.Map;

@Service
public class ResidentServiceImpl implements ResidentService {

    @Autowired
    private ResidentDao residentDao;

    /**
     * 调用分页
     * @param residents
     * @return
     */
    @Override
    public PageInfo<Residents> selectAllResidents(Residents residents) {
        //        设置分页规则
        PageHelper.startPage(residents.getPageNum(), 10);
        //        返回所有分页信息参数为查询所有记录的信息
        PageInfo<Residents> pageInfo = new PageInfo<>(residentDao.selectAllResidents(residents));
        return pageInfo;
        //return residentDao.selectAllResidents(residents);
    }

    @Override
    public List<Residents> selectResidentsByCondition(Map<String, String> mapCondition) {
        return residentDao.selectResidentsByCondition(mapCondition);
    }

    @Override
    public List<String> selectAllBuildingNo() {
        return residentDao.selectAllBuildingNo();
    }

    @Override
    public List<String> selectAllRoomNo() {
        return residentDao.selectAllRoomNo();
    }

    @Override
    public int addUser(Residents residents) {
        //设置id为唯一值
        residents.setId(residentDao.getMaxId()+1);
        residents.setUserId(residentDao.getMaxUserID()+1);
        residents.setCheckinDate(DateUtil.getCurrentDate());
        return residentDao.addUser(residents);
    }

    @Override
    public int deleteUserById(int id) {
        return residentDao.deleteUserById(id);
    }

    @Override
    public int updateUser(Residents residents) {
        return residentDao.updateUser(residents);
    }

    @Override
    public int updateUserByUser(Residents residents) {
        return residentDao.updateUserByUser(residents);
    }

    @Override
    public Residents getResidentById(int id) {
        return residentDao.getResidentById(id);
    }

    @Override
    public int deleteMultipleResident(List<Integer> ids) {
        return residentDao.deleteMultipleResident(ids);
    }

    @Override
    public Residents getResidentByUserId(int userId) {
        return residentDao.getResidentByUserId(userId);
    }
}
